define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'warehouse/index' + location.search,
                    add_url: 'warehouse/add',
                    edit_url: 'warehouse/edit',
                    del_url: 'warehouse/del',
                    multi_url: 'warehouse/multi',
                    import_url: 'warehouse/import',
                    table: 'warehouse',
                }
            });

            var table = $("#table");

            //外键关联查询处理
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $('form', table.$commonsearch);
                $("input[name='supermarket_id']", form)
                    .addClass('selectpage')
                    .data("source", "Supermarkets/index")
                    .data('primaryKey', "id")
                    .data('select-only', true);
                if ($(".selectpage", form).size() > 0) {
                    Form.events.selectpage(form);
                }
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate: false},
                        {field: 'supermarket_id', title: __('Supermarket_id'),visible: false},

                        {field: 'supermarket.name', title: __('Supermarket_id'),operate: false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'capacity', title: __('Capacity')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
